import QtQuick
import QtQuick.Controls.Material

Item {
    id: styledInput
    width: inputWidth
    height: inputHeight

    // 基本尺寸属性
    property real inputWidth: 200
    property real inputHeight: 40

    // 核心功能属性
    property string text: ""  // 存储数字字符串
    property bool isEditing: false     // 编辑状态
    property bool isSelected: false    // 选中状态
    property bool isPassword: false    // 密码模式

    // 样式属性
    property string normalColor: "transparent"
    property string hoverColor: "transparent"
    property string pressedColor: "transparent"
    property string selectedColor: "#FFFACD"
    property string selectedBorderColor: "#FFD700"

    property string normalBorderColor: "#bdc3c7"
    property string hoverBorderColor: "#3498db"
    property string pressedBorderColor: "#3498db"
    property string editingBorderColor: "#3498db"

    // 字体样式
    property real fontPixelSize: 25
    property string fontColor: "#2c3e50"
    property bool fontBold: false

    // 布局样式
    property real borderWidth: 1
    property real borderRadius: 0
    property real innerPadding: 10

    // 密码模式相关
    property string passwordMask: "*"

    // 功能信号
    signal valueChanged(string newValue)
    signal editingFinished(string finalValue)
    signal editCancelled()

    // 背景容器
    Rectangle {
        id: inputContainer
        anchors.fill: parent
        radius: borderRadius
        border.width: borderWidth
        border.color: isSelected ? selectedBorderColor
                    : isEditing ? editingBorderColor
                    : (mainMouseArea.hovered ? hoverBorderColor : normalBorderColor)  // 使用整合后的hover状态
        color: isSelected ? selectedColor
              : isEditing ? pressedColor
              : (mainMouseArea.hovered ? hoverColor : normalColor)
    }

    // 显示模式组件（移除内部MouseArea，由主MouseArea统一处理点击）
    Text {
        id: displayText
        visible: !isEditing
        anchors.fill: parent
        text: isPassword && styledInput.text.length > 0
              ? passwordMask.repeat(styledInput.text.length)
              : styledInput.text || "点击输入数字"
        font.pixelSize: fontPixelSize
        font.bold: fontBold
        color: styledInput.text ? fontColor : "#4682B4"
        horizontalAlignment: Text.AlignHCenter
        verticalAlignment: Text.AlignVCenter
        padding: innerPadding
        elide: Text.ElideMiddle
        maximumLineCount: 1
    }

    // 输入模式组件
    TextInput {
        id: inputField
        visible: isEditing
        anchors.centerIn: parent
        text: styledInput.text
        font.pixelSize: fontPixelSize
        font.bold: fontBold
        color: fontColor
        padding: innerPadding
        // alignment: Qt.AlignHCenter | Qt.AlignVCenter
        horizontalAlignment: Qt.AlignHCenter
        verticalAlignment: Qt.AlignVCenter
        selectionColor: selectedColor
        selectedTextColor: fontColor

        echoMode: isPassword ? TextInput.Password : TextInput.Normal

        // 数字输入限制
        validator: DoubleValidator {
            bottom: -Infinity
            top: Infinity
            decimals: 10
        }
        inputMethodHints: Qt.ImhFormattedNumbersOnly

        // 焦点状态变化处理
        onFocusChanged: {
            if (!focus && isEditing) {
                Qt.callLater(confirmEdit);
            }
        }

        // 按键事件（显式参数）
        Keys.onEnterPressed: function(keyEvent) {
            confirmEdit();
            keyEvent.accepted = true;
        }
        Keys.onReturnPressed: function(keyEvent) {
            confirmEdit();
            keyEvent.accepted = true;
        }
        Keys.onEscapePressed: function(keyEvent) {
            cancelEdit();
            keyEvent.accepted = true;
        }
    }

    // 整合所有MouseArea功能的主MouseArea
    MouseArea {
        id: mainMouseArea
        anchors.fill: parent
        hoverEnabled: true  // 始终启用悬停检测（通过状态控制是否生效）
        property bool hovered: false  // 整合原hoverDetector的hovered状态

        // 悬停逻辑：仅在非编辑状态下更新hovered
        onEntered: {
            if (!isEditing) {
                hovered = true;
            }
        }
        onExited: {
            if (!isEditing) {
                hovered = false;
            }
        }

        // 点击逻辑：根据编辑状态执行不同操作
        onClicked: {
            if (!isEditing) {
                // 非编辑状态：进入编辑模式并让TextInput获取焦点
                isEditing = true;
                inputField.text = styledInput.text;
                Qt.callLater(() => {
                    if (inputField.visible) {
                        inputField.forceActiveFocus();
                        Qt.callLater(inputField.selectAll);
                    }
                });
            } else {
                // 编辑状态：点击区域时结束编辑（如果输入框有焦点）
                if (inputField.focus) {
                    inputField.focus = false;
                }
            }
        }
    }

    // 确认编辑
    function confirmEdit() {
        if (isEditing) {
            const newText = inputField.text.trim() || styledInput.text;
            if (newText !== styledInput.text) {
                styledInput.text = newText;
                valueChanged(newText);
            }
            editingFinished(newText);
            isEditing = false;
            inputField.focus = false;
        }
    }

    // 取消编辑
    function cancelEdit() {
        if (isEditing) {
            inputField.text = styledInput.text;
            editCancelled();
            isEditing = false;
            inputField.focus = false;
        }
    }
}
